<?php
namespace  App\Baqi\Alipayapp;

use App\Baqi\Alipayapp\AopClient;
use App\Baqi\Alipayapp\AlipayTradeAppPayRequest;

class AlipayApp 
{
    //支付宝网关地址
    public $gateway_url = "https://openapi.alipay.com/gateway.do";

    //支付宝公钥
    public $alipay_public_key = '';

    //商户私钥
    public $private_key = '';

    //应用id
    public $appid = '';

    //编码格式
    public $charset = "UTF-8";

    public $token = NULL;
    
    //返回数据格式
    public $format = "json";

    //签名方式
    public $signtype = "RSA2";

    //同步回调
    public $return_url = '';
    //异步回调
    public $notify_url = '';

    // 业务参数集合
    public $setBizContent = '';

    //超时时间
    public $timeout_express = "10m";

    // 产品标示码，固定值：QUICK_WAP_PAY
    public $productCode = 'QUICK_WAP_PAY';

    //api版本
    public $apiVersion = "1.0";

    // 表单提交字符集编码
    public $postCharset = "UTF-8";

    public $method = "alipay.trade.wap.pay";

    //设置参数
    public function __construct($config) {
        //dd($config);
        $this->alipay_public_key = $config['public_key'];
        $this->private_key       = $config['private_key'];
        $this->appid             = $config['appid'];
        $this->notify_url        = isset($config['notify_url']) ? $config['notify_url'] : '';

        //讲业务参数
        $json_params = json_encode($config,JSON_UNESCAPED_UNICODE);
    }


    public function  getSign($params) {
        $aop = new AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $this->appid;
        $aop->rsaPrivateKey = $this->private_key;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = $this->signtype;
        $aop->alipayrsaPublicKey = $this->alipay_public_key;      

        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request    = new AlipayTradeAppPayRequest();


        $bizcontent = $this->setBizcontent($params);

        $request->setNotifyUrl($this->notify_url);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        //echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
        return $response;
    }

    public function setBizcontent($data) 
    {
        $params['body']             = $data['body'];
        $params['subject']          = $data['subject'];
        $params['out_trade_no']     = $data['order_sn'];
        $params['timeout_express']  = $this->timeout_express;
        $params['total_amount']     = $data['money'];
        $params['product_code']     = 'QUICK_MSECURITY_PAY';    

        $bizcontent = json_encode($params, JSON_UNESCAPED_UNICODE);

        return $bizcontent;
    }


    public function check ($data) 
    {
        $aop = new AopClient;
        $aop->alipayrsaPublicKey = $this->alipay_public_key;
        $flag = $aop->rsaCheckV1($data, NULL, $this->signtype);
        return $flag;
    }
}
?>